package com.chiwen.web.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chiwen.common.core.domain.AjaxResult;
import com.chiwen.common.core.domain.entity.SysPayway;
import com.chiwen.common.core.domain.entity.SysUser;
import com.chiwen.common.utils.BigDecimalUtil;
import com.chiwen.common.utils.DateUtils;
import com.chiwen.common.utils.ServletUtils;
import com.chiwen.common.utils.StringUtils;
import com.chiwen.common.utils.http.CommonHttp;
import com.chiwen.common.utils.http.HttpUtils;
import com.chiwen.common.utils.http.PayWayUtil;
import com.chiwen.common.utils.ip.IpUtils;
import com.chiwen.ks.domain.KS_QUERY_ORDER;
import com.chiwen.quartz.domain.SysJob;
import com.chiwen.quartz.service.ISysJobService;
import com.chiwen.system.domain.SysOrder;
import com.chiwen.system.service.ISysOrderService;
import com.chiwen.system.service.ISysPaywayService;
import com.chiwen.system.service.ISysUserService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 定时任务调度测试
 *
 * @author Dipper
 */
@Component("sysKsOrderTask")
public class SysKsOrderTask {

    @Autowired
    private ISysOrderService sysOrderService;
    @Autowired
    private ISysJobService sysJobService;
    @Autowired
    private ISysPaywayService iSysPaywayService;
    @Autowired
    private ISysUserService iSysUserService;


    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void orderStatus(String merchantId, String outOrderNo, String ip) {

        //头信息，多个头信息多次调用此方法即可
        String result = PayWayUtil.KS_Order_Query(merchantId, outOrderNo, ip);
        KS_QUERY_ORDER data = JSON.parseObject(result, KS_QUERY_ORDER.class);

        SysOrder sysOrder = sysOrderService.selectSysOrderByOrderCode(outOrderNo);

        if (data.getCode().equals("SUCCESS")) {
            int datePoorMin = DateUtils.getDatePoorMin(DateUtils.getNowDate(), sysOrder.getCreateTime());
            if (data.getOrder_state().equals("SUCCESS")) {

                SysPayway sysPayway = iSysPaywayService.selectSysPaywayByWayCode(sysOrder.getPayWay());

                String moeny =  BigDecimalUtil.addString(sysPayway.getMoeny(),sysOrder.getOrderMoney());
                sysPayway.setMoeny(moeny);
                sysPayway.setSuccessNum((Integer.parseInt(sysPayway.getSuccessNum()) + 1) + "");
                iSysPaywayService.updateSysPayway(sysPayway);

                SysUser user = iSysUserService.selectUserById(Long.valueOf(sysOrder.getCreateBy()));
                user.setSuccessNum((Integer.parseInt(user.getSuccessNum()) + 1) + "");
                iSysUserService.updateUser(user);

                sysJobService.deleteJobByOrderCode("%orderStatus%"+outOrderNo);
                // 如果成功 修改订单状态， 创建 回调方法！
                sysOrder.setOrderStage("3");
                sysOrder.setPayMoney(sysOrder.getOrderMoney());
                sysOrder.setPayTime(DateUtils.getNowDate());
                sysOrder.setUpdateTime(DateUtils.getNowDate());
                sysOrder.setCallbackCount(1);


                Map<String, String> callRest = sysOrderService.sendCallBack(sysOrder);

                 if(!String.valueOf(callRest.get("code")).equals("200")){
                     String s ="sysKsOrderTask.orderCallBack('"+outOrderNo+"')";
                     sysJobService.createJob(s,"0/30 * * * * ?");
                     sysOrder.setCallbackStage("回调失败，失败次数:1");
                 }else {
                     sysOrder.setCallbackStage("回调成功");
                 }

                sysOrderService.updateSysOrder(sysOrder);

            } else if (datePoorMin > 2) {
                // 不成功，判断时间是否超过2分钟，超过 修改订单状态 删除 定时任务
                sysJobService.deleteJobByOrderCode("%orderStatus%"+outOrderNo);


                SysUser user = iSysUserService.selectUserById(Long.valueOf(sysOrder.getCreateBy()));
                String moeny =  BigDecimalUtil.addString(user.getRemaMoney(),sysOrder.getOrderMoney());
                user.setRemaMoney(moeny);
                iSysUserService.updateUser(user);

                // 如果成功 修改订单状态， 创建 回调方法！
                sysOrder.setOrderStage("4");
                sysOrder.setUpdateTime(DateUtils.getNowDate());
                sysOrderService.updateSysOrder(sysOrder);
            }
        }

    }


    public void orderCallBack( String outOrderNo) {
        System.out.println("执行无参方法");

        SysOrder sysOrder = sysOrderService.selectSysOrderByOrderCode(outOrderNo);

        int count = sysOrder.getCallbackCount();
        sysOrder.setCallbackCount(++count);
        sysOrderService.updateSysOrder(sysOrder);
        if(count>=5){
            sysOrder.setCallbackStage("回调失败，失败次数:"+count);
            sysJobService.deleteJobByOrderCode("%orderCallBack%"+outOrderNo);
        }
        Map<String, String> callRest = sysOrderService.sendCallBack(sysOrder);

        if(String.valueOf(callRest.get("code")).equals("200")){
            sysOrder.setCallbackStage("回调成功");
            sysJobService.deleteJobByOrderCode("%orderCallBack%"+outOrderNo);
        }else{
            sysOrder.setCallbackStage("回调失败，失败次数:"+count);
        }

        sysOrderService.updateSysOrder(sysOrder);

    }
}
